package com.wenda.admin.core;

import jpcap.JpcapCaptor;
import jpcap.NetworkInterface;

import java.util.HashMap;
import java.util.Map;

public class NetworkUtils {

    /**
     * 获取所有网卡设备
     * @return
     */
   public static Map<String,NetworkInterface> getAllNetwork() {
       Map<String,NetworkInterface> result = new HashMap<>();
       NetworkInterface[] deviceList = JpcapCaptor.getDeviceList();
       for (NetworkInterface temp:deviceList){
           String name = temp.description + " " + temp.addresses[0].address.getHostAddress();
           result.put(name,temp);
       }
        return result;
    }

    public static Map<String,Integer> getAllNetworkSeq() {
        Map<String,Integer> result = new HashMap<>();
        NetworkInterface[] deviceList = JpcapCaptor.getDeviceList();
        for (int i=0;i<deviceList.length;i++){
            NetworkInterface temp = deviceList[i];
            String name = temp.description + " " + temp.addresses[0].address.getHostAddress();
            result.put(name,i);
        }
        return result;
    }

    public static void main(String[] args){
        Map<String, NetworkInterface> allNetwork = getAllNetwork();
        allNetwork.keySet().stream().forEach(System.out::println);
    }

}
